package GuardTest;

import sun.rmi.runtime.Log;

import java.io.IOException;
import java.security.Timestamp;
import java.util.List;
import java.util.Timer;

public class GuardObject {
    public static void main(String[] args) {
        GuardObject guardObject=new GuardObject();
        new Thread(()->{
            System.out.println("t1等待结果");
            List<String> list=(List<String>) guardObject.get();
            System.out.println("结果大小"+list.get(1));
        }).start();
        new Thread(()->{
            System.out.println("t2执行下载");
            try {
                List<String> list=DownloadTest.download();
                guardObject.complete(list);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }).start();
    }
    private Object resp;

    /**
     * 获取结果
     * @return
     */
    public Object get(){
        synchronized (this){
        while(resp == null){
                    try {
                        this.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
        }
        return  resp;
    }


    public void complete(Object resp){
        synchronized (this){
            this.resp=resp;
            this.notifyAll();
        }
    }

}
